﻿package plugin.juezhan.controller
{
    import __AS3__.vec.*;
    import base.*;
    import base.component.*;
    import base.titlebar.*;
    import com.core.*;
    import com.sh.game.net.*;
    import com.sh.game.util.*;
    import consts.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;
    import manager.*;
    import morn.core.handlers.*;
    import plugin.component.view.*;
    import plugin.instance.*;
    import plugin.juezhan.model.*;
    import plugin.juezhan.ui.*;
    import pluginfw.nio.*;
    import view.light.*;
    import view.mornExtends.*;
    import view.uilt.*;
    import vo.*;

    public class JueZhanTianGuanControl extends Object
    {
        private var mUI:JueZhanUIControl;
        private const ITEM_COUNT:uint = 6;
        private var TATAL_COUNT:int = 0;
        private var mProgress:Array;
        private var mSelecteIndex:int;
        private var mlayer:int;
        private var mColorFilter:ColorMatrixFilter;
        private var mTianGuansCfg:Array;
        public var mRankControl:TianGuanRankControl;
        private var mrews:Array;
        private var role:RoleShow;
        private var _container:Sprite;
        private var _titlemanager:TitleBarManager;
        private var items:Vector.<ITitleItem>;
        private var nameSp:LinearLabel;
        private var curdata:Object;
        private var light1:Boolean = false;

        public function JueZhanTianGuanControl(param1:JueZhanUIControl)
        {
            var _loc_5:headitemUIControl = null;
            this.mProgress = [0, 0];
            this.mrews = [];
            this.items = new Vector.<ITitleItem>;
            this.mUI = param1;
            this.mTianGuansCfg = ConfigDictionary.data.juezhantianguan;
            var _loc_2:int = 1;
            while (_loc_2 < this.mTianGuansCfg.length)
            {
                
                var _loc_6:String = this;
                var _loc_7:* = this.TATAL_COUNT + 1;
                _loc_6.TATAL_COUNT = _loc_7;
                _loc_5 = new headitemUIControl();
                _loc_5.addEventListener(MouseEvent.CLICK, this.headitemClickHandler);
                this.items.push(_loc_5);
                _loc_2++;
            }
            this.listener();
            var _loc_3:* = new ColorMatrix();
            _loc_3.adjustColor(0, 0, -100, 0);
            this.mColorFilter = new ColorMatrixFilter(_loc_3);
            this.mRankControl = new TianGuanRankControl(this.mUI);
            this._container = new Sprite();
            this._container.x = 287;
            this._container.y = 320;
            this._container.mouseChildren = false;
            this._container.mouseEnabled = false;
            this.ui.addChildAt(this._container, param1.numChildren - 4);
            param1.imgTGFirst.visible = true;
            var _loc_4:* = new Shape();
            new Shape().graphics.beginFill(16777215, 1);
            _loc_4.graphics.drawRect(0, 0, 112, 446);
            _loc_4.graphics.endFill();
            _loc_4.y = 62;
            _loc_4.x = 12;
            this.ui.addChild(_loc_4);
            this.ui.tabbox.mask = _loc_4;
            param1.label_weizhi3.visible = false;
            return;
        }// end function

        protected function headitemClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this.items.indexOf(event.currentTarget as ITitleItem);
            this.setSelectIndex((_loc_2 + 1));
            return;
        }// end function

        private function listener() : void
        {
            this.ui.btn_shang.addEventListener(MouseEvent.CLICK, this.prePageHandler);
            this.ui.btn_xia.addEventListener(MouseEvent.CLICK, this.nextPageHandler);
            this.ui.btnTGChallenge.addEventListener(MouseEvent.CLICK, this.challengeHandler);
            this.ui.btn_lingqu.addEventListener(MouseEvent.CLICK, this.getRewardHandler);
            return;
        }// end function

        protected function getRewardHandler(event:MouseEvent) : void
        {
            var _loc_2:* = new Output();
            _loc_2.writeVarInt((this._titlemanager._index + 1), false);
            PluginManager.instance.sendToBufferServer("juezhan", CMDJueZhan.ReqTGRewards, _loc_2);
            return;
        }// end function

        public function update() : void
        {
            Server.instance.send(this.ui.groupID, CMDJueZhan.TIAN_GUAN_PROGRESS, null);
            Server.instance.send(this.ui.groupID, CMDJueZhan.TGRankInit, null);
            return;
        }// end function

        public function updateProgress(param1:Array) : void
        {
            var _loc_6:ITitleItem = null;
            var _loc_7:Boolean = false;
            var _loc_8:Object = null;
            var _loc_9:String = null;
            this.mProgress = param1;
            this.curdata = this.mTianGuansCfg[this.mProgress[0]][this.mProgress[1]];
            this.ui.label_weizhi.text = "第" + this.curdata.id + "重";
            this.ui.label_weizhi1.text = "第" + this.curdata.smallid + "层";
            this.ui.label_weizhi3.text = "第" + this.curdata.id + "重" + ",第" + this.curdata.smallid + "层";
            this.ui.shuomingLabel.text = this.curdata.baoshitips;
            var _loc_2:* = (this.curdata.name as String).split(":");
            var _loc_3:* = ConfigDictionary.data.instance[this.curdata.instanceid];
            var _loc_4:* = (_loc_3.limit_times as String).split(" ");
            this.ui.label_tzcs.text = _loc_4[1] - this.mProgress[2] + "/" + _loc_4[1];
            if (_loc_3.open_level > UserData.getUserData("level"))
            {
                this.ui.label_lv.color = MColor.New56;
            }
            else
            {
                this.ui.label_lv.color = MColor.New61;
            }
            if (_loc_3.rein > 0)
            {
                this.ui.label_lv.text = _loc_3.rein + "转";
            }
            else
            {
                this.ui.label_lv.text = _loc_3.open_level + "级";
            }
            var _loc_5:int = 0;
            while (_loc_5 < this.items.length)
            {
                
                _loc_6 = this.items[_loc_5];
                _loc_7 = false;
                if ((_loc_5 + 1) > this.curdata.id)
                {
                    _loc_7 = true;
                }
                _loc_8 = {name:"第" + (_loc_5 + 1) + "重", lock:_loc_7};
                (_loc_6 as headitemUIControl).data = _loc_8;
                _loc_5++;
            }
            if (this.mProgress[0] <= 0)
            {
                this.mSelecteIndex = 1;
                this.setSelectIndex(1);
            }
            else
            {
                for (_loc_9 in this.mProgress[3])
                {
                    
                    if (this.mProgress[3][_loc_9] == 0)
                    {
                        this.mSelecteIndex = int(_loc_9);
                        this.setSelectIndex(int(_loc_9));
                        this.updateSelected();
                        return;
                    }
                }
                this.mSelecteIndex = param1[0];
                this.setSelectIndex(param1[0]);
            }
            this.updateSelected();
            if (this.nameSp == null)
            {
                this.nameSp = new LinearLabel(180, 35, [14786337, 16777011], [1, 1], [0, 255]);
                this.nameSp.textFormat = new TextFormat("宋体", 14, null, true, null, null, null, null, TextFormatAlign.CENTER);
                this.nameSp.text = "第" + this.curdata.id + "重" + ",第" + this.curdata.smallid + "层";
                this.nameSp.filters = [FilterDefine.miaobian, FilterDefine.yelloGlow2];
                this.nameSp.x = 201;
                this.nameSp.y = 103;
                this.ui.addChild(this.nameSp);
                this.nameSp.draw();
            }
            else
            {
                this.nameSp.text = "第" + this.curdata.id + "重" + ",第" + this.curdata.smallid + "层";
                this.nameSp.draw();
            }
            return;
        }// end function

        public function rankInit(param1:Object) : void
        {
            this.mRankControl.rankInit(param1);
            return;
        }// end function

        private function selectHandler(param1:int) : void
        {
            this.mSelecteIndex = param1 + 1;
            this.updateSelected();
            return;
        }// end function

        private function updateSelected() : void
        {
            var _loc_8:int = 0;
            this.mlayer = this.mProgress[1];
            this.ui.btn_lingqu.disabled = false;
            if (this.mProgress[3][this.mSelecteIndex] != 0)
            {
                this.ui.cbox.visible = true;
                _loc_8 = 20 - this.mlayer + 1;
                if (_loc_8 >= 10)
                {
                    this.ui.cimg0.url = "png.common." + int(_loc_8 / 10);
                    this.ui.cimg1.url = "png.common." + int(_loc_8 % 10);
                    this.ui.cimg0.visible = true;
                }
                else
                {
                    this.ui.cimg0.visible = false;
                    this.ui.cimg1.url = "png.common." + _loc_8;
                }
                this.light1 = false;
                LightFactory.clearlight(this.ui.btn_lingqu);
                this.ui.btn_lingqu.disabled = true;
            }
            else
            {
                this.ui.cbox.visible = false;
                this.light1 = true;
                LightFactory.showlight(this.ui.btn_lingqu);
            }
            if (this.curdata.id == this.mSelecteIndex)
            {
                this.ui.btnTGChallenge.disabled = false;
            }
            else
            {
                this.ui.btnTGChallenge.disabled = true;
            }
            if (this.mProgress[3][this.mSelecteIndex] == 1)
            {
                this.ui.imgTGFirst.filters = [this.mColorFilter];
            }
            else
            {
                this.ui.imgTGFirst.filters = [];
            }
            var _loc_1:* = ConfigDictionary.data.item;
            var _loc_2:* = this.mTianGuansCfg[this.mSelecteIndex][this.mlayer];
            var _loc_3:* = String(_loc_2.firstdrop).split("#");
            var _loc_4:* = ItemObject.create(_loc_1[_loc_3[0]]);
            ItemObject.create(_loc_1[_loc_3[0]]).count = _loc_3[1];
            this.ui.item_rew.data = _loc_4;
            this.ui.label_jl.text = _loc_4.name;
            this.mrews.length = 0;
            var _loc_5:* = String(_loc_2.drop).split("/");
            var _loc_6:int = 0;
            while (_loc_6 < _loc_5.length)
            {
                
                _loc_4 = ItemObject.create(_loc_1[String(_loc_5[_loc_6]).split("#")[0]]);
                if (String(_loc_5[_loc_6]).split("#").length > 1)
                {
                    _loc_4.count = String(_loc_5[_loc_6]).split("#")[1];
                }
                this.mrews.push(_loc_4);
                _loc_6++;
            }
            this.ui.list.array = this.mrews;
            this.ui.list.renderHandler = new Handler(this.rewsHalder);
            var _loc_7:* = (_loc_2.name as String).split(":");
            this.ui.labelTGName.text = _loc_7[1];
            this.ui.labelTGPower.text = "推荐战力" + _loc_2.power;
            this.showModel();
            this.ui.linkrank.visible = _loc_2.model != "";
            (this.ui.zhanshibox as ZhanshiCtrl).model = _loc_2.model;
            return;
        }// end function

        public function showModel() : void
        {
            if (this.mTianGuansCfg[this.mSelecteIndex] == null)
            {
                return;
            }
            var _loc_1:* = this.mTianGuansCfg[this.mSelecteIndex][this.mlayer];
            if (this.role)
            {
                this.role.updateModel(_loc_1.img);
            }
            else
            {
                this.role = new RoleShow(_loc_1.img, 2, null, null, null);
                this._container.addChild(this.role);
            }
            if (this.light1)
            {
                LightFactory.showlight(this.ui.btn_lingqu);
            }
            return;
        }// end function

        public function clearModel() : void
        {
            if (this.role)
            {
                if (this.role.parent)
                {
                    this.role.parent.removeChild(this.role);
                }
                this.role.destroy();
                this.role = null;
            }
            LightFactory.clearlight(this.ui.btn_lingqu);
            return;
        }// end function

        private function rewsHalder(param1:FlashItem, param2:int) : void
        {
            if (param2 < this.ui.list.array.length)
            {
                param1.data = this.ui.list.array[param2];
            }
            return;
        }// end function

        public function setSelectIndex(param1:int) : void
        {
            if (param1 <= 0)
            {
                param1 = this.TATAL_COUNT;
            }
            else if (param1 > this.TATAL_COUNT)
            {
                param1 = param1 - this.TATAL_COUNT;
            }
            if (this._titlemanager == null)
            {
                this._titlemanager = new TitleBarManager(this.ui.tabbox, this.TATAL_COUNT, 67, 2, (param1 - 1), 6, this.selectHandler);
                this._titlemanager.addItems(this.items);
            }
            else
            {
                this._titlemanager.setSelect((param1 - 1));
            }
            return;
        }// end function

        private function prePageHandler(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            if (this.mSelecteIndex <= 1)
            {
                _loc_2 = this.TATAL_COUNT;
            }
            else
            {
                _loc_2 = this.mSelecteIndex - 1;
            }
            this.setSelectIndex(_loc_2);
            return;
        }// end function

        private function nextPageHandler(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            if (this.mSelecteIndex >= this.TATAL_COUNT)
            {
                _loc_2 = 1;
            }
            else
            {
                _loc_2 = this.mSelecteIndex + 1;
            }
            this.setSelectIndex(_loc_2);
            return;
        }// end function

        private function challengeHandler(event:MouseEvent) : void
        {
            var _loc_2:* = UserData.getData(UserDataType.Task);
            var _loc_3:* = this.curdata.instanceid;
            var _loc_4:* = ConfigDictionary.data.instance[_loc_3];
            if (ConfigDictionary.data.instance[_loc_3].open_level > ModelProxy.getValue("data", "user.level"))
            {
                WarningManager.show(38, [_loc_4.open_level]);
                return;
            }
            if (_loc_4.rein > ModelProxy.getValue("data", "user.rein"))
            {
                WarningManager.show(39, [_loc_4.rein]);
                return;
            }
            var _loc_5:* = (_loc_4.limit_times as String).split(" ");
            if ((_loc_4.limit_times as String).split(" ")[1] - this.mProgress[2] == 0)
            {
                WarningManager.show(46);
                return;
            }
            PluginManager.instance.sendToServer("instance", CMDInstance.NpcInstance, {mapid:this.curdata.instanceid, type:0});
            this.ui.close();
            return;
        }// end function

        private function get ui() : JueZhanUIControl
        {
            return this.mUI;
        }// end function

        public function restgrewards(param1:int) : void
        {
            this.mProgress[3][param1] = 1;
            if (param1 >= this.TATAL_COUNT)
            {
                this.updateSelected();
                return;
            }
            this.mSelecteIndex = param1 + 1;
            this.setSelectIndex((param1 + 1));
            this.updateSelected();
            return;
        }// end function

    }
}
